import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait  # 导入等待类
from selenium.webdriver.support import expected_conditions as EC  # 导入条件类
import src.util.nacos.nacos_client as nacos_client   # 导入nacos模块init
import pyautogui
import os
import pyperclip
from src.infoinit import choice

#pyautogui.FAILSAFE=False
s=True
def click_upload(driver,json):
    main_dir_path = nacos_client.get_value('filePath') + json['caseNumber']
    print(main_dir_path)
    # 遍历每个文件
    time.sleep(1)
    pyautogui.click(185, 802)
    pyautogui.keyDown('ctrl')
    pyautogui.scroll(-1000)
    pyautogui.scroll(-1000)
    pyautogui.keyUp('ctrl')
    for file in json['fileInfo']:
        dir_name = f"{file['scanCatalogue']}_{file['headingCatalogue']}"
        dir_path = os.path.join(main_dir_path, dir_name)
        print(dir_path)
        # 定位到上传按钮并点击
        upload_button = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "upload_add")))
        upload_button.click()
        # 等待上传窗口打开
        WebDriverWait(driver, 20).until(EC.new_window_is_opened)
        time.sleep(0.5)
        pyautogui.hotkey("alt","d")
        time.sleep(0.5)
        pyperclip.copy(dir_path)
        # 输入文件夹路径
        pyautogui.hotkey("ctrl","v")
        #pyautogui.write(r"C:\test\案号\扫描目录名称1_归目目录名称1")
        time.sleep(0.5)
        # 按下并松开回车键，进入文件夹
        pyautogui.press("enter")
        time.sleep(0.5)
        pyautogui.press("tab")
        pyautogui.press("tab")
        pyautogui.press("tab")
        pyautogui.press("tab")
        # 全选文件
        pyautogui.hotkey("ctrl", "a")
        time.sleep(0.5)
        # 按下并松开回车键，确定选择的文件上传
        pyautogui.press("enter")

        time.sleep(2)
        
        
        # 创建一个字典
        data = {
            "scanCatalogue": file["scanCatalogue"],
            "headingCatalogue": file["headingCatalogue"]
        }
        choice(driver,data)
        time.sleep(1)
        pyautogui.press('f5')
        time.sleep(1)
